﻿//
// PolyLineGraphGrid.xaml.cpp
// Implementation of the PolyLineGraphGrid class
//

#include "pch.h"
#include "PolyLineGraphGrid.xaml.h"
#include "Constants.h"

using namespace PolyLineGraph2;

using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;


// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236


PolyLineGraphGrid::PolyLineGraphGrid()
{
	InitializeComponent();
}

void PolyLineGraphGrid::UpdatePolyLineGraphGrid(double containerWidth, double containHeight, double maxValue, int numOfVals)
{
	//polyLineGraphGridPanel->Background = ref new SolidColorBrush(Windows::UI::Colors::Orange);
	polyLineGraphGridPanel->Width = containerWidth;
	polyLineGraphGridPanel->Height = containHeight;
	polyLineGraphGridPanel->Children->Clear();

	if(numOfVals != 0)
	{
		int NumOfRowGrid = 7;
		int gridHeight = containHeight - TopBlank - BottomBlank;
		int gridWidth = containerWidth - LeftBlank - RightBlank;
		double heightgap = gridHeight / NumOfRowGrid;
		//double weightgap = gridWidth / (2 * numOfVals);

		//lines
		Windows::UI::Xaml::Shapes::Line^ bottomline = ref new Windows::UI::Xaml::Shapes::Line();
		Windows::UI::Xaml::Shapes::Line^ leftline = ref new Windows::UI::Xaml::Shapes::Line();
		Windows::UI::Xaml::Shapes::Line^ rightline = ref new Windows::UI::Xaml::Shapes::Line();
		Windows::UI::Xaml::Shapes::Line^ topline = ref new Windows::UI::Xaml::Shapes::Line();

		bottomline->X1 = LeftBlank;
		bottomline->Y1 = TopBlank + gridHeight;
		bottomline->X2 = LeftBlank + gridWidth;
		bottomline->Y2 = bottomline->Y1;
		bottomline->StrokeThickness=2;
		bottomline->Stroke = ref new SolidColorBrush(Windows::UI::Colors::Gray);

		leftline->X1 = bottomline->X1;
		leftline->Y1 = TopBlank;
		leftline->X2 = bottomline->X1;
		leftline->Y2 = bottomline->Y1;
		leftline->StrokeThickness=2;
		leftline->Stroke = ref new SolidColorBrush(Windows::UI::Colors::Gray);

		rightline->X1 = bottomline->X2;
		rightline->Y1 = bottomline->Y2;
		rightline->X2 = rightline->X1;
		rightline->Y2 = leftline->Y1;
		rightline->StrokeThickness=2;
		rightline->Stroke = ref new SolidColorBrush(Windows::UI::Colors::Gray);
		rightline->Opacity = 0.6;

		topline->X1 = rightline->X2;
		topline->Y1 = rightline->Y2;
		topline->X2 = leftline->X1;
		topline->Y2 = leftline->Y1;
		topline->StrokeThickness=2;
		topline->Stroke = ref new SolidColorBrush(Windows::UI::Colors::Gray);
		topline->Opacity = 0.8;

		polyLineGraphGridPanel->Children->Append(topline);
		polyLineGraphGridPanel->Children->Append(bottomline);
		polyLineGraphGridPanel->Children->Append(leftline);
		polyLineGraphGridPanel->Children->Append(rightline);

		Windows::UI::Xaml::Shapes::Line^ midline;

		for(unsigned int i = 1; i < NumOfRowGrid; i++)
		{
			midline = ref new Windows::UI::Xaml::Shapes::Line();
			midline->X1 = leftline->X1;
			midline->Y1 = leftline->Y1 + i * heightgap;
			midline->X2 = rightline->X1;
			midline->Y2 = midline->Y1;
			midline->StrokeThickness = 2;
			midline->Stroke = ref new SolidColorBrush(Windows::UI::Colors::Gray);
			midline->Opacity = 0.3;
			polyLineGraphGridPanel->Children->Append(midline);
		}

		double valueGap = gridHeight / NumOfRowGrid;
		double actualValueGap = maxValue / NumOfRowGrid;
		Windows::UI::Xaml::Controls::TextBlock^ valueText;
		for(unsigned int i = 0; i < NumOfRowGrid + 1; i++)
		{
			valueText = ref new Windows::UI::Xaml::Controls::TextBlock();
			valueText->HorizontalAlignment = Windows::UI::Xaml::HorizontalAlignment::Left;
			valueText->VerticalAlignment = Windows::UI::Xaml::VerticalAlignment::Top;
			double d1 = maxValue - actualValueGap * i;
			wchar_t chars[60];
			swprintf_s(chars,L"%6.2f",d1);
			valueText->Text = ref new String(chars);
			valueText->Height = valueGap;
			valueText->FontSize = 20;
			valueText->Foreground = ref new SolidColorBrush(Windows::UI::Colors::Black);
			if(i == 0)
			{
				double tmp_double = topline->Y1 - 6;
				valueText->Margin = Windows::UI::Xaml::Thickness(12, topline->Y1 - 6, 0, 0);
			}
			else
			{
				double tmp_double = topline->Y1 - 6 + i * valueGap;
				valueText->Margin = Windows::UI::Xaml::Thickness(12, topline->Y1 - 6 + i * valueGap, 0, 0);
			}
			polyLineGraphGridPanel->Children->Append(valueText);
		}

	}
}